import os
from PIL import Image
import random

def circle(img_path):
    # path_name = os.path.dirname(img_path)
    # print(path_name)
    # cir_file_name = 'cir_img.png'
    # cir_path = path_name + '/' + cir_file_name
    # print(cir_path)
    # if os.path.isfile(cir_path):
    #     print("l")
    cir_path="F:/PythonWorkSpace/slice_pic/cir_img.png"
    ima = Image.open(cir_path).convert("RGBA")
    size = ima.size
    print(size)
    # 因为是要圆形，所以需要正方形的图片
    r2 = min(size[0], size[1])
    if size[0] != size[1]:
        ima = ima.resize((r2, r2), Image.ANTIALIAS)

    # 删除
    Image.new('RGBA', (r2/2))
    # 最后生成圆的半径
    r3 = int(r2/2)
    imb = Image.new('RGBA', (r3*2, r3*2),(255,255,255,0))
    pima = ima.load() # 像素的访问对象
    pimb = imb.load()
    r = float(r2/2) #圆心横坐标
 
    for i in range(r2):
        for j in range(r2):
            lx = abs(i-r) #到圆心距离的横坐标
            ly = abs(j-r)#到圆心距离的纵坐标
            l = (pow(lx,2) + pow(ly,2))** 0.5 # 三角函数 半径
            if l < r3:
                pimb[i-(r-r3),j-(r-r3)] = pima[i,j]
 
    imb.save(cir_path)
    return cir_path

# 分割几行几列, 二维数组保存
def SplitImages(img_path, row, col):
    path_name = os.path.dirname(img_path)
    img = Image.open(img_path).convert("RGBA")
    imgSize = img.size

    splitW = int(imgSize[0]/col)
    splitL = int(imgSize[1]/row)

    pimg = img.load()

    imbList = []
    for i in range(row):
        rowList = []
        l = (i + 1) * splitL
        for j in range(col):
            w = (j + 1) * splitW
            imb = Image.new('RGBA', (splitW, splitL),(255,255,255,0))
            pimb = imb.load()

            for k in range(j * splitW, w):
                for z in range(i * splitL, l):
                    pimb[k - splitW * j, z - i * splitL] = pimg[k,z]
            dirPath = path_name + "/" + str(i*10 + j) + ".png"
            # imb.save(dirPath)
            rowList.append(imb)
        imbList.append(rowList)

    return imbList

def Resize(img, rizeW, rizel, pastePoint=None): 
    if pastePoint is None:
        pastePoint = [0, 0]

    new_im = Image.new('RGBA', [rizeW, rizel],(255,255,255,0))
    new_im.paste(img, pastePoint)

    return new_im


def SplitCircle(imbList, imgPath):
    path_name = os.path.dirname(imgPath)
    img = Image.open(imgPath).convert("RGBA")
    imgSize = img.size

    col = len(imbList[0])
    row = len(imbList)

    if col == 1 and row == 1:
        return 

    splitW = int(imgSize[0]/col)
    splitL = int(imgSize[1]/row)

    minV = min(splitW, splitL)
    r_d = int(minV / 4) # 要计算 两个不能比 l 长 并且加上 offset 也不能超过 l
    r_offset = int(minV / 8)

    pSplitW = splitW + (r_d + r_offset) * 2
    pSplitL = splitL + (r_d + r_offset) * 2

    pimg = img.load()
    
    # 存(row - 1) * (col - 1) 个中心点
    pointList = []
    for i in range(row):
        colPointList = []
        for j in range(col):
            colPoint = []
            rowPoint = []
            if j != col - 1:
                colPoint = [splitW * (j + 1), int(splitL/2) + i * splitL]
            if i != row - 1:
                rowPoint = [int(splitW / 2) + j * splitW, splitL * (i + 1)]
            colPointList.append({'colPoint': colPoint, 'rowPoint': rowPoint})

            imbList[i][j] = Resize(imbList[i][j], pSplitW, pSplitL, [r_d + r_offset, r_d + r_offset])
            # dirPath = path_name + "/" + str(i*10 + j) + ".png"
            # imbList[i][j].save(dirPath)
        pointList.append(colPointList)

    for i in range(row):
        for j in range(col):
            imbImg = imbList[i][j]
            new_img = imbImg

            # 圆心靠左 靠右, 默认靠右
            lrandNum = random.randint(0, 999999)
            drandNum = random.randint(0, 999999)
            lrRight = True
            drRight = True
        
            if lrandNum < 500000:
                lrRight = False
            if drandNum < 500000:
                drRight = False

            new_img_imb = new_img.load()

            if j != col - 1:

                if lrRight :
                    new_next_img = imbList[i][j + 1]
                    new_next_img_imb = new_next_img.load()
                    # 左右
                    for k in range((j + 1) * splitW, (j + 1) * splitW + r_d + r_offset):
                        for z in range(i * splitL, (i + 1) * splitL):
                            r_w = pointList[i][j]['colPoint'][0] + r_offset
                            r_l = pointList[i][j]['colPoint'][1] 
                            r = ((pow(abs(k - r_w),2) + pow(abs(z - r_l),2))) ** 0.5
                            if r < r_d:
                                new_img_imb[k - j * splitW + r_d + r_offset, z - i * splitL + r_d + r_offset] = pimg[k, z]
                                new_next_img_imb[k - (j + 1) * splitW + r_d + r_offset, z - i * splitL + r_d + r_offset] = (255,255,255,0)

                    imbList[i][j + 1] = new_next_img
                    # dirPath = path_name + "/" + str(i*10 + j) + "kk.png"
                    # new_next_img.save(dirPath)
                    # dirPath = path_name + "/" + str(i*10 + j) + "tt.png"

                    # new_img.save(dirPath)
                else:
                    new_next_img = imbList[i][j + 1]
                    new_next_img_imb = new_next_img.load()
                    # 左右
                    for k in range((j + 1) * splitW - r_d - r_offset, (j + 1) * splitW):
                        for z in range(i * splitL, (i + 1) * splitL):
                            r_w = pointList[i][j]['colPoint'][0] - r_offset
                            r_l = pointList[i][j]['colPoint'][1]
                            r = ((pow(abs(k - r_w),2) + pow(abs(z - r_l),2))) ** 0.5
                            if r < r_d:
                                new_next_img_imb[k - (j + 1) * splitW + r_d + r_offset, z - i * splitL + r_d + r_offset] = pimg[k, z]
                                new_img_imb[k - j * splitW  + r_d + r_offset, z - i * splitL + r_d + r_offset] = (255,255,255,0)

                    imbList[i][j + 1] = new_next_img  

                    # dirPath = path_name + "/" + str(i*10 + j) + "kk.png"
                    # new_next_img.save(dirPath)
                    # dirPath = path_name + "/" + str(i*10 + j) + "tt.png"

                    # new_img.save(dirPath)          

            if i!= row - 1:

                if drRight:
                    new_down_img = imbList[i + 1][j]
                    new_down_img_imb = new_down_img.load()
                    # 上下
                    for k in range(j * splitW, (j + 1) * splitW):
                        for z in range((i + 1) * splitL, (i + 1) * splitL + r_d + r_offset):
                            r_w = pointList[i][j]['rowPoint'][0]
                            r_l = pointList[i][j]['rowPoint'][1] + r_offset
                            r = ((pow(abs(k - r_w),2) + pow(abs(z - r_l),2))) ** 0.5
                            if r < r_d:
                                new_img_imb[k - j * splitW + r_d + r_offset, z - i * splitL + r_d + r_offset] = pimg[k, z]
                                new_down_img_imb[k - j * splitW + r_d + r_offset, z - (i + 1) * splitL + r_d + r_offset] = (255,255,255,0)
            
                    imbList[i + 1][j] = new_down_img
                    # dirPath = path_name + "/" + str(i*10 + j) + "kkk.png"
                    # new_down_img.save(dirPath)
                    # dirPath = path_name + "/" + str(i*10 + j) + "ttt.png"

                    # new_img.save(dirPath)
                else:
                    new_down_img = imbList[i + 1][j]

                    new_down_img_imb = new_down_img.load()
                    # 上下
                    for k in range(j * splitW, (j + 1) * splitW):
                        for z in range((i + 1) * splitL - r_d - r_offset, (i + 1) * splitL):
                            r_w = pointList[i][j]['rowPoint'][0]
                            r_l = pointList[i][j]['rowPoint'][1] - r_offset
                            r = ((pow(abs(k - r_w),2) + pow(abs(z - r_l),2))) ** 0.5
                            if r < r_d:
                                new_down_img_imb[k - j * splitW + r_d + r_offset, z - (i + 1) * splitL + r_d + r_offset] = pimg[k, z]
                                new_img_imb[k - j * splitW + r_d + r_offset, z - i * splitL + r_d + r_offset] = (255,255,255,0)

                    imbList[i + 1][j] = new_down_img

                    # dirPath = path_name + "/" + str(i*10 + j) + "kk.png"
                    # new_down_img.save(dirPath)
                    # dirPath = path_name + "/" + str(i*10 + j) + "tt.png"

                    # new_img.save(dirPath)          
        
            imbList[i][j] = new_img
        
    for i in range(row):
        for j in range(col):
            dirPath = path_name + "/" + str(i*10 + j) + ".png"
            imbList[i][j].save(dirPath)




if __name__ == '__main__':
    # circle("")
    dirPath = "E:/PythonWorkSpace/split_pic/"
    ima = Image.open(dirPath + "a.png").convert("RGBA")

    SplitCircle(SplitImages(dirPath + "a.png", 6, 4) , dirPath + "a.png")


    
   
